﻿using Dapper.Sharding;
using Newtonsoft.Json;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Test
{
    class TestClient
    {

        public static IClient Client = ShardingFactory.CreateClient(DataBaseType.MySql, new DataBaseConfig { Server = "127.0.0.1", UserId = "root", Password = "123456", Port = 3306 });

        [Test]
        public async Task A()
        {
            var db1 = Factory.Client.GetDatabase("test1");
            var db2 = Factory.Client.GetDatabase("test2");
            var db3 = Client.GetDatabase("test");

            var tran = ShardingFactory.CreateDistributedTransaction();
            try
            {
                db1.Execute("insert into people(name)values('db1')", tran: tran);

                await db1.ExecuteAsync("insert into people(name)values('db11')", tran: tran);

                //await db2.ExecuteAsync("insert into people(name)valuse('db2')", tran: tran);
                db2.Execute("insert into people(name)values('db2')", tran: tran);

                await db3.ExecuteAsync("insert into people(name)values('db3')", tran: tran);

                await tran.CommitAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);

                var tranStatus = await tran.RollbackAsync();
                foreach (var item in tranStatus)
                {
                    Console.WriteLine(item.Key.Name + $"==>{item.Value}");
                }
            }
        }

        [Test]
        public void CreateDatabase()
        {
            Factory.Client.CreateDatabase("demo");
        }

        [Test]
        public void DropDatabase()
        {

            Factory.Client.DropDatabase("demo");
        }


        [Test]
        public void GetDatbase()
        {
            Factory.Client.GetDatabase("demo");
        }

        [Test]
        public void ExistDatabase()
        {
            bool exists = Factory.Client.ExistsDatabase("demo");
            Console.WriteLine(exists);
        }

        [Test]
        public void ShowDatabases()
        {
            var databases = Factory.Client.ShowDatabases();
            Console.WriteLine(JsonConvert.SerializeObject(databases));
        }

        [Test]
        public void ShowDatabasesWithOutSystem()
        {
            var databases = Factory.Client.ShowDatabasesExcludeSystem();
            Console.WriteLine(JsonConvert.SerializeObject(databases));
        }

        [Test]
        public void ClearCache()
        {
            Factory.Client.ClearCache();
        }

        [Test]
        public void NextId()
        {
            var id = ShardingFactory.NextLongIdAsString();
            var id2 = ShardingFactory.NextObjectId();
            var id3 = ShardingFactory.NextSnowIdAsString();
            Assert.Pass($"{id}\n{id2}\n{id3}");
        }

        [Test]
        public async Task TestThreadDb()
        {
            var task1 = Task.Run(() =>
            {
                return Factory.Client.GetDatabase("zzz1");
            });

            var task2 = Task.Run(() =>
            {
                return Factory.Client.GetDatabase("zzz1");
            });

            await Task.WhenAll(task1, task2);
            Console.WriteLine(task1.Result.Equals(task2.Result));
            Console.WriteLine("没问题");

        }

        [Test]
        public async Task TestThreadTable()
        {
            var task1 = Task.Run(() =>
            {
                return Factory.Client.GetDatabase("zzz1").GetTable<People>("p");
            });

            var task2 = Task.Run(() =>
            {
                return Factory.Client.GetDatabase("zzz1").GetTable<People>("p");
            });

            await Task.WhenAll(task1, task2);
            Console.WriteLine(task1.Result.Equals(task2.Result));
            Console.WriteLine("没问题");
        }

        [Test]
        public void LoadConfig()
        {
            var c1 = ConfigSystemTextJson.LoadConfig("db.json");

            var c3 = ConfigSystemTextJson.LoadConfig("db.json", "node");

            var c5 = ConfigSystemTextJson.LoadConfigList("db.json", "list");

            var c7 = ConfigSystemTextJson.LoadConfigList("db2.json");

            var client1 = ShardingFactory.CreateClient("db.json");
            var client2 = ShardingFactory.CreateClient("db.json", "node");
            var clientList = ShardingFactory.CreateClientList("db.json", "list");
            var clientDict = ShardingFactory.CreateClientDict("db.json", "list");

        }

        [Test]
        public async Task GetDatabaseByConnString()
        {
            var db = Factory.Client.GetDatabaseExists("demo");
            db.GetTable<Student>("dddd");

            db = await Factory.Client.GetDatabaseAsync("demo");
            db.GetTable<Student>("dddd2");

            db = Factory.Client.GetDatabaseByConnString("Host=127.0.0.1;uid=postgres;pwd=123;database=demo");
            db.GetTable<Student>("dddd3");

            await db.GetTableAsync<Student>("dddd4");
        }

        [Test]
        public void Tablespace()
        {
            Factory.Client.GetDatabase("test2", "ts_test");
        }

        [Test]
        public void CreateTablespace()
        {
            Factory.Client.CreateTablespace("ts_1", "D:\\B");
        }

        [Test]
        public void DropTablespace()
        {
            Factory.Client.DropTablespace("ts_1");
        }

    }


}
